package com.woniu.inlet.web;

import com.woniu.inlet.fo.ProAddFo;
import com.woniu.inlet.fo.ProductFo;
import com.woniu.outlet.mysql.po.Product;
import com.woniu.service.productcommand.*;
import com.woniu.util.ObjectUtil;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: my
 * @time: 2021/4/26 19:26
 */
@RestController
@CrossOrigin
@Slf4j
@Api("理财产品接口")
public class ProductController {

    @Autowired
    private ObjectUtil objectUtil;

    /**
     * 根据id获取理财产品信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/product/wealth", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("根据id获取理财信息")
    public ResponseResult getById(Integer id) {
        if (id == null || id == 0){
            id = 1;
        }
        GetProductCommand command = new GetProductCommand(id);
        Product product = command.execute();
        return new ResponseResult(product, 200);
    }

    @RequestMapping(value = "/product/wealth/all", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("根据条件获取理财信息")
    @ApiImplicitParams({
                               @ApiImplicitParam(name = "name", value = "产品名称", required = false),
                               @ApiImplicitParam(name = "type", value = "产品类型", required = false),
                               @ApiImplicitParam(name = "startTime", value = "起始期限", required =
                                       false),
                                @ApiImplicitParam(name = "endTime",value = "终止期限",required = false)
                       })
    public ResponseResult queryProducts(String name, String type, Integer startTime, Integer endTime) {
        ProductFo productFo = new ProductFo();
        if (name != null && !"".equals(name.trim())) {
            productFo.setName(name);
        }
        if (type != null && !"".equals(type.trim())) {
            log.debug("type:"+type);
            productFo.setType(type);
        }
        if (startTime != null && startTime >= 0) {
            productFo.setStartTime(startTime);
        }
        if (endTime != null && endTime >=0) {
            productFo.setEndTime(endTime);
        }
        QueryProductsCommand command = new QueryProductsCommand(productFo);
        List<Product> list = command.execute();
        return new ResponseResult(list, 200);
    }

    @RequestMapping(value = "/product/wealth/update",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("产品信息修改方法")
    public ResponseResult updateProduct(Product product){
        if(product.getStatus() != null && "y".equals(product.getStatus())){
            return new ResponseResult(400,"该商品未下架");
        }
        UpdateProductCommand command = new UpdateProductCommand(product);
        command.execute();
        return ResponseResult.SUCCESS;
    }

    @RequestMapping(value = "/product/wealth/add",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("产品信息添加方法")
    public ResponseResult addProduct(ProAddFo proAddFo){
        boolean b = objectUtil.checkAllFieldIsNotNull(proAddFo);
        if (!b){
            return new ResponseResult(400,"参数不能为空");
        }
        AddProductCommand command = new AddProductCommand(proAddFo);
        command.execute();
        return ResponseResult.SUCCESS;
    }

    @RequestMapping(value = "/product/wealth/manage",method = {RequestMethod.POST,
            RequestMethod.GET})
    @ApiOperation("产品上下架方法")
    public ResponseResult productManage(Integer id){
        if (id == null){
            return new ResponseResult(400,"参数缺失");
        }
        try {
            ProductManageCommand command = new ProductManageCommand(id);
            command.execute();
            return new ResponseResult(200,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.FAIL;
        }
    }

}
